﻿<div ng-controller="AddLockOrderC">
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">客户选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" />-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" ng-change="FindAllBy()">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style=" width: 100%; height: 200px;border:1px solid #CCCCCC;overflow:auto;">
                        <div style="cursor:pointer;" class="form-control" ng-click="selectEmp(x)" ng-repeat=" x in client|filter:{ShortName : searchTxt}|filter:{Name : searchTxt}">{{x.ShortName}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">添加锁价单</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive" ng-show="isshow">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label>客户（必填）</label>
                                <!--<select required ui-jq="chosen" class="form-control" ng-options="x.Name for x in client" ng-model="client1"></select>-->
                                <!--<label class="form-control" ng-model="orderdata.ClientManager.Name" type="text" data-toggle="modal" data-target="#dlg">-->
                                    <!--{{orderdata.ClientManager.Name}}-->
                                <!--</label>-->
                                <button required="required" class="form-control text-left" type="text" data-toggle="modal" data-target="#dlg">
                                    {{client1.ShortName}}
                                </button>
                            </div>
                            <div class="form-group">
                                <label>户头</label>
                                <select class="form-control" ng-model="orderdata.AccountId" name="District">
                                    <option ng-repeat="y in client1.Accounts" value="{{y.Id}}">{{y.Name}}</option>
                                </select>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label style="margin-right: 10px;">是否标件</label>-->
                                <!--<label class="i-switch bg-info">-->
                                    <!--<input type="checkbox" ng-model="orderdata.IsStandard">-->
                                    <!--<i></i>-->
                                <!--</label>-->
                            <!--</div>-->
                            <div class="form-group">
                                <div class="checkbox">
                                    <label for="yijia" style="margin-right: 10px;">是否议价</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="yijia" ng-model="orderdata.IsBargaining">
                                        <i></i>
                                    </label>
                                    <label for="suojia" style="margin-right: 10px;">是否商品锁价</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="suojia" ng-model="orderdata.IsGoodsLock">
                                        <i></i>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group" ng-if="orderdata.IsBargaining">
                                <label>单价优惠金额</label>
                                <input ng-model="orderdata.ApplyDiscount" type="number" class="form-control" required placeholder="请输入议价金额">
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label style="margin-right: 10px;">是否商品锁价</label>-->
                                <!--<label class="i-switch bg-info">-->
                                    <!--<input type="checkbox" ng-model="orderdata.IsGoodsLock">-->
                                    <!--<i></i>-->
                                <!--</label>-->
                            <!--</div>-->
                            <div class="form-group">
                                <label>锁价单总额</label>
                                <input type="number" ng-model="orderdata.Total" min="0" class="form-control" required placeholder="请输入锁价金额">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input ng-model="orderdata.Remarks" type="text" class="form-control" placeholder="请输入备注信息（100字内）" maxlength="100">
                            </div>
                            <div class="form-group">
                                <label>供应商</label>
                                <select ng-change="clearCommodity()" class="form-control" required ng-model="orderdata.ProviderId" name="cangku">
                                    <option ng-repeat="y in cangku" value="{{y.Id}}">{{y.Name}}——{{y.ProviderType==0?"钢厂":(y.ProviderType==1?"仓库":"贸易商") }}</option>
                                </select>
                            </div>
                        </div>
                        <div ng-if="orderdata.IsGoodsLock">
                        <div class="container-fluid text-center bg-light lter" >
                            <!--<div class="row">-->
                                <!--<h3 style="display: inline-block;margin: 0 auto">锁价商品（限一件）<button type="button" class="btn btn-success" ng-click="xxx()">去选商品</button></h3>-->
                            <!--</div>-->
                            <div class="row" style="height: 40px;">
                                <div class="col-xs-6 col-md-6 text-left">
                                    <strong style="display: inline-block;line-height: 40px">锁价商品（限一种规格）</strong>
                                </div>
                                <a>
                                    <div ng-click="xxx()" class="col-xs-6 col-md-6 text-right" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">去选商品&nbsp;&nbsp;
                                        <i class="fa fa-chevron-right text"></i>
                                    </span>
                                    </div>
                                </a>
                            </div>
                            <!--<div class="row text-right">-->
                                <!--<button type="button" class="btn btn-sm btn-success" ng-click="xxx()">去选商品</button>-->
                            <!--</div>-->
                        </div>
                        <div class="container-fluid" style="padding-top: 10px">
                            <!--<div class="row" style="border-bottom: red solid 1px" ng-repeat="item in commodity">-->
                                <!--<div class="col-xs-9 col-md-9">-->
                                    <!--<div>钢厂：{{item.Commodity.Brand}}</div>-->
                                    <!--<div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>-->
                                    <!--<div>单价：{{item.Price | number:2}}/t</div>-->
                                <!--</div>-->
                                <!--<div class="col-xs-3 col-md-3" style="text-align: center;">-->
                                    <!--<button type="button" class="btn btn-sm btn-danger"  ng-click="deduct(item)" style="margin: 5px">-->
                                        <!--删除-->
                                    <!--</button>-->
                                <!--</div>-->
                            <!--</div>-->

                            <div class="panel panel-default" ng-repeat="item in commodity" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>品牌：{{item.Commodity.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                            <div>单价：{{item.Price | number:2}}/t</div>
                                        </div>
                                        <div class="col-xs-3 col-md-3" style="text-align: center;">
                                            <button type="button" class="btn btn-sm btn-danger"  ng-click="deduct(item)" style="margin: 5px">
                                                删除
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                         <!--<div class="row" style="text-align: center;padding: 18px 0;">-->
                                <!--<button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交锁价单</button>-->
                                <!--<span style="padding:0 10%"></span>-->
                                <!--<button type="button" class="btn btn-default" ng-click="abolish()">返回列表页</button>-->
                         <!--</div>-->
                        <div class="row text-center" style="height: 40px;">
                            <a>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-default" ng-click="abolish()">返回列表页</button>
                                </div>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-success" ng-disabled="form.$invalid||bing" ng-click="ordersub()">提交锁价单</button>
                                </div>
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div ng-show="!isshow">
                <div class="container-fluid bg-light lter" style="margin-bottom: 20px;">
                    <!--<div class="btn-group dropdown" dropdown>-->
                        <!--<button class="btn btn-default" dropdown-toggle>请选择商品类型 <span class="caret"></span></button>-->
                        <!--<ul class="dropdown-menu">-->
                            <!--<li ng-click="genre('盘螺')"><a href>盘螺</a></li>-->
                            <!--<li ng-click="genre('线材')"><a href>线材</a></li>-->
                            <!--<li ng-click="genre('钢坯')"><a href>钢坯</a></li>-->
                            <!--<li ng-click="genre('螺纹钢')"><a href>螺纹钢</a></li>-->
                        <!--</ul>-->
                    <!--</div>-->
                    <!--<button type="button" style="margin-left: 30px;" class="btn btn-success" ng-click="xxx()">返回购物车</button>-->

                    <div class="row" style="height: 40px;">
                        <a>
                            <div ng-click="xxx()" class="col-xs-6 col-md-6 text-left" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">
                                        <i class="fa fa-chevron-left text"></i>&nbsp;&nbsp;返回订单
                                    </span>
                            </div>
                        </a>
                        <div class="col-xs-6 col-md-6 text-center">
                            <select style="" class="btn btn-default" ng-model="findName" ng-change="genre(findName)">
                                <option value="盘螺">盘螺</option>
                                <option value="线材">线材</option>
                                <option value="钢坯">钢坯</option>
                                <option value="螺纹钢">螺纹钢</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="container-fluid" style="margin-top: 10px">
                    <div class="panel panel-default" ng-repeat="item in ProviderCommodity" style="margin-bottom: 10px">
                        <div class="panel-heading">
                            <div>钢厂：{{item.Commodity.Brand}}</div>
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-xs-9 col-md-9">
                                    <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                    <div>价格：{{item.Price | number:2}}</div>
                                </div>
                                <div class="col-xs-3 col-md-3" style="text-align: center;">
                                    <button type="button" ng-show="!item.isadd" class="btn btn-sm btn-default" ng-click="add(item)">
                                        <i class="fa fa-plus text"></i>
                                        <span class="text">添加</span>
                                    </button>
                                    <button type="button" ng-show="item.isadd" class="btn btn-sm btn-default" ng-click="deduct(item)">
                                        <i class="fa fa-minus text"></i>
                                        <span class="text">取消</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

<script>
    angular.module('app') //添加所加单控制器
    .controller("AddLockOrderC" , function($scope,$state,$http){
        $scope.isshow = true;
        $scope.isadd = true;
        $scope.findName = '盘螺';
        $scope.commodity = [];
        $scope.selectedDistrict1 = "";
        $scope.client1 = "";
        $scope.searchTxt = "";
        $scope.FindAllBy = function () {
          
            $http({
                url: "../api/ClientManager/GetCustomer?name=" + $scope.searchTxt,
                method: "GET"
            })
           .then(function (res) {
               if (res.State <= 0) {
                   alert(res.Msg);
               } else {
                   $scope.client = res.data;
                   console.log($scope.client);
               }
           })
           .then(function (error) {
               if (error) {
                   console.log("error");
               }
           });
        }
        $scope.FindAllBy();
        //获取仓库列表
        $http({
            url:"../api/Provider?page=1&rows=9999",
            method:"GET"
        })
                .then(function (res) {
                    $scope.cangku = res.data.Data;
                    console.log($scope.cangku)
                })
                .then(function (error) {
                    if(error){
                        console.log("error");
                    }
                });
        $scope.selectEmp= function(emp) {
            $scope.client1 = emp;
            $('#dlg').modal("hide");
        };
        //提交编辑结果
        $scope.xxx = function () {
           
            if ($scope.orderdata.ProviderId) {
                $scope.findName = '盘螺';
                $scope.genre('盘螺');
                $scope.isshow = !$scope.isshow;
            }else {
                alert("请选择仓库");
            }
           
        };
        $scope.genre = function (name) {
            if(name != "" && $scope.orderdata.ProviderId != ""){
                //获取仓库商品列表
                $http({
                    url: "../api/Provider/DepotCommodity?depotId=" + $scope.orderdata.ProviderId + "&name=" + name + "&format=&page=1&rows=9999",
                    method:"GET"
                })
                        .then(function (res) {
                            $scope.ProviderCommodity = res.data;
                            console.log($scope.ProviderCommodity);
                        })
                        .then(function (error) {
                            if(error){
                                console.log("error");
                            }
                        });
            }
        };
        //添加到购物车
        $scope.add = function(item){
            if ($scope.commodity.length < 1) {
                if (item.Price==0) {
                    alert("商品价格未定价，请联系你的管理员");
                }
                else{
                    $scope.commodity.push(item);
                    item.isadd = true;
                }
                }else {
                    alert("只能选择一件商品");
                }
        };

        $scope.clearCommodity = function (item) {
            $scope.commodity = [];
        };


        //仓库改变清空购物车
        $("#Provider").change(function(){
            $scope.commodity=[];
        });
        //从购物车删除
        $scope.deduct = function(item){
            item.isadd = false;
            $scope.commodity=[];
        };
        $scope.orderdata = {};
        console.log($scope.orderdata)
        //提交计划单
        $scope.ordersub = function(){
            // console.log($scope.orderdata);
            for (var i = 0; i < $scope.cangku.length; i++) {
                if ($scope.cangku[i].Id==$scope.orderdata.ProviderId) {
                    $scope.long=$scope.cangku[i];
                    break;
                };
            }
            if ($scope.long.MainFieldMan!=null&&$scope.long.Manager!=null&&$scope.long.OperationsMan!=null) {
                $scope.orderdata.CustomerId = $scope.client1.Id;
                $scope.orderdata.depotCommodityRecordId=null;
                if($scope.orderdata.IsGoodsLock){
                    $scope.orderdata.depotCommodityRecordId = $scope.commodity[0].CommodityId;
                }
                $http.post('../api/LockOrder/Add', $scope.orderdata)
                        .success(function (res) {
                            if (res.State <= 0) {
                                alert(res.Msg);
                            } else {
                                alert("添加成功");
                                $state.go('app.MyLockOrder_List');
                            }
                            
                });
            }else{
                alert('请先完善供应商信息')
            }
           
        };
        //返回上一页
        $scope.abolish = function () {
            $state.go('app.MyLockOrder_List');
        };
        $scope.empty = function(){
            $scope.searchTxt = "";
        };
    })

</script>